This repository contains two PowerShell Core scripts designed for comprehensive npm package analysis and security scanning across multiple projects.
- Scan-NpmPackages.ps1: Scans directory trees for npm packages and generates detailed reports
- Scan-ReportsForPackages.ps1: Security scanner that identifies affected packages from watchlists
- PowerShell Core 7.0 or later
- Windows, macOS, or Linux
- Projects with
package.jsonand/orpackage-lock.jsonfiles
Scans directory structures for npm packages and creates comprehensive reports including all dependencies and transitive dependencies.
- Scans
package.jsonfiles for dependency information - Processes
package-lock.jsonfiles by default for complete dependency trees (1,600+ packages vs 50 direct dependencies) - Supports all dependency types: regular, dev, peer, and optional
- Generates CSV reports with detailed package information
- Handles npm lock file formats v1, v2, and v3
- Warns when lock files are missing for optimal dependency analysis
Root(Required): Root directory to scan for npm projectsOutDir: Output directory for reports (default:./npm-package-report)SkipLockFiles: Skip lock files and use only package.json (not recommended)IncludeDev: Include devDependenciesIncludePeer: Include peerDependenciesIncludeOptional: Include optionalDependenciesIncludeDependencyTree: Recursively scan dependency treesShowVersionSpecs: Show version specifications instead of installed versionsMaxDepth: Maximum recursion depth (default: 10)
# Basic scan of all projects under C:\repos (uses lock files by default)
.\scripts\Scan-NpmPackages.ps1 -Root "C:\repos"
# Comprehensive scan with all dependency types
.\scripts\Scan-NpmPackages.ps1 -Root "C:\repos" -IncludeDev -IncludePeer -IncludeOptional
# Scan with custom output directory
.\scripts\Scan-NpmPackages.ps1 -Root "C:\repos" -OutDir ".\reports"
# Deep dependency analysis with version specifications
.\scripts\Scan-NpmPackages.ps1 -Root "C:\repos" -IncludeDependencyTree -ShowVersionSpecs -MaxDepth 15
# Use only package.json files (skip lock files - not recommended)
.\scripts\Scan-NpmPackages.ps1 -Root "C:\repos" -SkipLockFiles -IncludeDevpackages_aggregate.csv: Summary of all unique packages across projectspackages_detailed.csv: Detailed view with project locations and dependency types- Individual project reports:
project-name_packages.csv
Security scanning tool that identifies affected packages from watchlists or vulnerability databases.
- Reads CSV reports generated by Scan-NpmPackages.ps1
- Matches packages against security watchlists
- Supports multiple input methods: files, parameters, or pipeline
- Generates security-focused reports with affected package detection
- Handles version comparisons and mismatches
- Color-coded output for security assessment
ReportsDir(Required): Directory containing CSV reports from Scan-NpmPackages.ps1PackageList: Array of packages to check againstPackageListFile: Path to file containing package watchlistAggregateReportFile: Name of aggregate report file (default:packages_aggregate.csv)DetailedReportFile: Name of detailed report file (default:packages_detailed.csv)OutputFile: Save results to CSV fileShowNotFound: Display watchlist packages not found in reportsShowVersionMismatches: Highlight version differencesIncludeSecurityInfo: Include security context in output
Watchlist entries can be provided in several formats:
- Simple package names:
chalk - With versions:
chalk@5.3.0 - Tab-separated:
chalk 5.3.0 High-risk package - With descriptions:
debug 4.4.2 CVE-2023-1234 vulnerability
# Scan reports for specific affected packages
.\scripts\Scan-ReportsForPackages.ps1 -ReportsDir ".\reports" -PackageList @("chalk", "debug", "lodash")
# Use a security watchlist file
.\scripts\Scan-ReportsForPackages.ps1 -ReportsDir ".\reports" -PackageListFile "security-watchlist.txt"
# Pipeline input for dynamic security scanning
$vulnerabilities = @(
"chalk 5.3.0 High-risk package",
"debug 4.4.2 CVE-2023-1234",
"lodash 4.17.20 Security vulnerability"
)
$vulnerabilities | .\scripts\Scan-ReportsForPackages.ps1 -ReportsDir ".\reports"
# Comprehensive security analysis with file output
.\scripts\Scan-ReportsForPackages.ps1 -ReportsDir ".\reports" -PackageListFile "watchlist.txt" -ShowNotFound -ShowVersionMismatches -IncludeSecurityInfo -OutputFile "security-scan-results.csv"
# Quick check for specific packages with custom report names
.\scripts\Scan-ReportsForPackages.ps1 -ReportsDir ".\reports" -PackageList @("express", "react") -AggregateReportFile "my-aggregate.csv" -DetailedReportFile "my-detailed.csv"# Step 1: Scan all projects for npm packages (lock files used by default)
.\scripts\Scan-NpmPackages.ps1 -Root "C:\repos" -OutDir ".\reports" -IncludeDev
# Step 2: Check against security watchlist
.\scripts\Scan-ReportsForPackages.ps1 -ReportsDir ".\reports" -PackageListFile "security-watchlist.txt" -ShowNotFound -IncludeSecurityInfo -OutputFile "security-results.csv"# Complete package analysis with all dependency types
.\scripts\Scan-NpmPackages.ps1 -Root "C:\repos" -OutDir ".\reports" -IncludeDev -IncludePeer -IncludeOptional -IncludeDependencyTree
# Security assessment with version mismatch analysis
.\scripts\Scan-ReportsForPackages.ps1 -ReportsDir ".\reports" -PackageListFile "enterprise-watchlist.txt" -ShowNotFound -ShowVersionMismatches -IncludeSecurityInfo -OutputFile "enterprise-security-report.csv"# Fast scan using lock files (default behavior)
.\scripts\Scan-NpmPackages.ps1 -Root "C:\repos"
# Check for specific CVEs
$cvePackages = @("debug 4.4.2 CVE-2023-1234", "chalk 5.3.0 CVE-2023-5678")
$cvePackages | .\scripts\Scan-ReportsForPackages.ps1 -ReportsDir ".\npm-package-report"Create a security-watchlist.txt file with entries like:
chalk 5.3.0 High-risk package with security issues
debug 4.4.2 CVE-2023-1234 - Information disclosure
lodash 4.17.20 Multiple security vulnerabilities
express 4.18.0 Security advisory GHSA-xxx
axios 0.21.0 Server-side request forgery
- packages_aggregate.csv: Unique packages with usage counts
- packages_detailed.csv: All package instances with project paths
- project-name_packages.csv: Individual project reports
- [OutputFile]: Affected packages detected in your projects
- [OutputFile]_not_detected.csv: Watchlist packages not found (good security posture)
- Lock files are used by default for complete dependency analysis
- Include all dependency types (
-IncludeDev,-IncludePeer,-IncludeOptional) - Regularly update watchlists with latest security advisories
- Monitor transitive dependencies as they represent the largest attack surface
- Use version mismatch analysis to identify outdated packages
- Save results to files for compliance and audit trails
- Generate lock files if missing (
npm install) for accurate analysis
- Lock files are used by default for faster and more accurate results
- Limit
-MaxDepthfor very large projects - Process reports in batches for large numbers of projects
- Use relative paths for portability across environments
- Use
-SkipLockFilesonly when necessary (reduces accuracy)
- Missing packages in results: Lock files are used by default for transitive dependencies
- Missing lock file warnings: Run
npm installto generate package-lock.json files - Performance issues: Reduce
-MaxDepthor disable-IncludeDependencyTree - File access errors: Run PowerShell as administrator if needed
- Encoding issues: Reports use UTF-8 encoding
Get-Help .\scripts\Scan-NpmPackages.ps1 -Full
Get-Help .\scripts\Scan-ReportsForPackages.ps1 -Full